package com.warm.base.jdk.thread.pool;

import java.util.LinkedList;

/**
 * @Date: 2020/4/3 13:54
 * @Author: LChuang
 * @Description: 任务队列
 */
public class RunnableTaskQueue {

    private final LinkedList<Runnable> tasks = new LinkedList<>();

    public Runnable getTask() throws InterruptedException {
        synchronized (tasks) {
            while (tasks.isEmpty()){
                System.out.println(Thread.currentThread().getName() + " tasks queue is empty. waiting...");
                tasks.wait();
            }
            return tasks.removeFirst();
        }
    }

    public void addTask(Runnable runnable){
        synchronized (tasks){
            tasks.add(runnable);
            tasks.notifyAll();
        }
    }
}
